package com.yilin.platform.controller.user;


import com.yilin.platform.base_core.application.BaseController;
import com.yilin.platform.base_core.application.PageData;
import com.yilin.platform.base_core.application.ReturnData;
import com.yilin.platform.base_core.commons.utils.ParamUtil;
import com.yilin.platform.base_core.commons.utils.StringUtil;
import com.yilin.platform.entity.user.Cooperate;
import com.yilin.platform.entity.user.Organize;
import com.yilin.platform.entity.user.auth.IdCard;
import com.yilin.platform.service.impl.user.AuthService;
import com.yilin.platform.service.impl.user.CooperateService;
import com.yilin.platform.service.impl.user.OrganizeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user/cooperate/")
public class CooperateController extends BaseController {
    @Resource
    private CooperateService cooperateService;
    @Resource
    private OrganizeService organizeService;
    @Resource
    private AuthService authService;
    @PostMapping("applyCooperate")
    @ResponseBody
    public ReturnData<Object> applyCooperate(Cooperate cooperate) {
        ParamUtil.isNull(cooperate.getCompanyOrganizeId(), "获取合作组织ID失败");
        IdCard idCard = authService.getIdCardByUserId(getLoginUser().getId());
        ParamUtil.isNull(idCard, "您还没有认证身份证，请先认证身份证");
        Organize organizeAccount = organizeService.getOrganizeById(getLoginUser().getOrganizeId());
        ParamUtil.isNull(organizeAccount.getId(), "获取您的组织失败");
        cooperate.setAccountOrganizeId(organizeAccount.getId());
        cooperate.setAccountOrganizeName(organizeAccount.getOrganizeName());
        cooperate.setAccountManagerUserPhone(organizeAccount.getManagerUserPhone());
        cooperate.setAccountManagerUserId(organizeAccount.getManagerUserId());
        cooperate.setAccountManagerUserName(organizeAccount.getManagerUserName());
        Organize organizeCompany = organizeService.getOrganizeById(cooperate.getCompanyOrganizeId());
        ParamUtil.isNull(organizeCompany.getId(), "获取您的合作组织失败");
        cooperate.setCompanyOrganizeName(organizeCompany.getOrganizeName());
        cooperate.setCompanyManagerUserPhone(organizeCompany.getManagerUserPhone());
        cooperate.setCompanyManagerUserId(organizeCompany.getManagerUserId());
        cooperate.setCompanyManagerUserName(organizeCompany.getManagerUserName());
        Cooperate cooperate1=cooperateService.getCooperateByCompanyId(organizeAccount.getId(),organizeCompany.getId());
        if(cooperate1!=null&&cooperate1.getCooperateState()!= Cooperate.CooperateState.cancellation){
            return jsonView(false, "已添加该合作组织");
        }else if(cooperate1!=null){
            cooperate.setId(cooperate1.getId());
        }
        cooperateService.applyCooperate(getLoginUser(), cooperate);
        return jsonView(true, "保存成功");
    }

    @PostMapping("agreeCooperate")
    @ResponseBody
    public ReturnData<Object> agreeCooperate(String cooperateId, Cooperate.CooperateState cooperateState) {
        ParamUtil.isNull(cooperateId, "请传入ID参数");
        ParamUtil.isNull(cooperateState, "请传入状态");
        if (cooperateState != Cooperate.CooperateState.cancellation && cooperateState != Cooperate.CooperateState.success) {
            return jsonView(false, "状态错误");
        }
        Cooperate cooperate = cooperateService.getCooperateById(cooperateId);
        ParamUtil.isNull(cooperate, "获取合作信息失败");
        cooperate.setCooperateState(cooperateState);
        cooperateService.agreeCooperate(getLoginUser(), cooperate);
        return jsonView(true, "保存成功");
    }

    @PostMapping("deleteCooperateById")
    @ResponseBody
    public ReturnData<Object> deleteCooperateById(String cooperateId) {
        ParamUtil.isNull(cooperateId, "请传入ID参数");
        cooperateService.deleteCooperateById(cooperateId);
        return jsonView(true, "删除成功");
    }

    @PostMapping("getCooperateById")
    @ResponseBody
    public ReturnData<Object> getCooperateById(String cooperateId) {
        ParamUtil.isNull(cooperateId, "请传入ID参数");
      Cooperate cooperate=  cooperateService.getCooperateById(cooperateId);
        return jsonView(true, "获取成功",cooperate);
    }

    @PostMapping("getCooperatePage")
    @ResponseBody
    public ReturnData<Object> getCooperatePage(String organizeId, Cooperate query, Integer page, Integer limit) {
        if (StringUtil.isEmpty(organizeId)) organizeId = getLoginUser().getOrganizeId();
        PageData<Cooperate> pageData = cooperateService.getCooperatePage(organizeId, query, page, limit);
        return jsonView(true, "获取成功", pageData);
    }

}
